package 力扣;

/**
 * @author yyq
 * @create 2022-03-05 12:24
 */
public class leetcode59 {
    public static void main(String[] args) {
        System.out.println(generateMatrix(3));

    }
    public static int[][] generateMatrix(int n) {
        int[][] nums=new int[n][n];
        int num=1;
        int left=0;
        int right=n-1;
        int top=0;
        int blow=n-1;
        while(num<=(n*n)){
            // left ---> right
            for(int i=left;i<=right;i++){
                nums[top][i]=num;
                num++;
            }
            top++;

            // top --->blow
            for (int i=top;i<=blow;i++){
                nums[i][right]=num;
                num++;
            }
            right--;

            // right --->left
            for(int i=right;i>=left;i--){
                nums[blow][i]=num;
                num++;
            }
            blow--;

            // blow ---> top
            for(int i=blow;i>=top;i--){
                nums[i][left]=num;
                num++;
            }
            left++;
        }
        return nums;
    }
}
